package com.exit66.jukebox.util;

/**
 * @author andyb
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Properties;
import java.util.StringTokenizer;

import com.exit66.jukebox.DemoSoundPlayer;
import com.exit66.jukebox.SoundPlayer;
import com.exit66.jukebox.Options;
import com.exit66.jukebox.data.Album;
import com.exit66.jukebox.data.AlbumCollection;
import com.exit66.jukebox.data.Artist;
import com.exit66.jukebox.data.ArtistCollection;
import com.exit66.jukebox.data.BVDatabaseCollection;
import com.exit66.jukebox.data.ContentType;
import com.exit66.jukebox.data.ContentTypeCollection;
import com.exit66.jukebox.data.FailedFileCollection;
import com.exit66.jukebox.data.Library;
import com.exit66.jukebox.data.LibraryCollection;
import com.exit66.jukebox.data.Maintenance;
import com.exit66.jukebox.data.Playlist;
import com.exit66.jukebox.data.PlaylistCollection;
import com.exit66.jukebox.data.PlaylistRuleCollection;
import com.exit66.jukebox.data.RequestQueue;
import com.exit66.jukebox.data.Track;
import com.exit66.jukebox.data.TrackCollection;

public class CommandParser {

	private static final String VARIABLE_BEGIN_TAG = "{@var:";

	private static final String VARIABLE_END_TAG = "}";

	private static final String ARTIST_ID_TAG = "{@artist_id}";

	private static final String ARTIST_NAME_TAG = "{@artist_name}";

	private static final String ARTIST_ORDER_NAME_TAG = "{@artist_order_name}";

	private static final String ALBUM_ID_TAG = "{@album_id}";

	private static final String ALBUM_NAME_TAG = "{@album_name}";

	private static final String ALBUM_MULTIPLE_ARTIST_ID_TAG = "{@multiple_artist_id}";

	private static final String ALBUM_ARTIST_ID_TAG = "{@album_artist_id}";

	private static final String ALBUM_ARTIST_NAME_TAG = "{@album_artist_name}";

	private static final String TRACK_ID_TAG = "{@track_id}";

	private static final String TRACK_NAME_TAG = "{@track_name}";

	private static final String TRACK_FILE_NAME_TAG = "{@track_file_name}";

	private static final String TRACK_NUMBER_TAG = "{@track_number}";

	private static final String TRACK_ARTIST_NAME_TAG = "{@track_artist_name}";

	private static final String LIBRARY_ID_TAG = "{@library_id}";

	private static final String LIBRARY_PATH_TAG = "{@library_path}";

	private static final String LIBRARY_TRACK_COUNT_TAG = "{@library_track_count}";

	private static final String LIBRARY_SCAN_STATUS_TAG = "{@scan_status}";

	private static final String LIBRARY_SCAN_STATUS_STRING_TAG = "{@scan_status_string}";

	private static final String CONTENT_TYPE_ID_TAG = "{@content_type_id}";

	private static final String CONTENT_TYPE_TAG = "{@content_type}";

	private static final String PLAYLIST_ID_TAG = "{@playlist_id}";

	private static final String PLAYLIST_NAME_TAG = "{@playlist_name}";

	private static final String PLAYLIST_RULE_ID_TAG = "{@playlist_rule_id}";

	private static final String PLAYLIST_RULE_TYPE_TAG = "{@rule_type}";
	
	private static final String PLAYLIST_RULE_VALUE_TAG = "{@rule_value}";
	
	private static final String PLAYLIST_RULE_DISPLAY_VALUE_TAG = "{@rule_display_value}";

	private static final String MESSAGE_TAG = "{@message}";

	String _outputBuffer;

	public CommandParser() {

		_outputBuffer = "";

	}

	public CommandParser(String templateFileName, Properties prop) {

		this();
		processTemplateFile(templateFileName, prop);

	}

	public String getOutput() {

		// remove the last character if it's a new line since that will be sent
		// by the
		// socket anyway
		/*
		 * if (_outputBuffer.length() > 0) {
		 * 
		 * if (_outputBuffer.substring(_outputBuffer.length() -
		 * NEWLINE_CHAR.length()).compareTo(NEWLINE_CHAR) == 0) {
		 * 
		 * _outputBuffer = _outputBuffer.substring(0, _outputBuffer.length() -
		 * NEWLINE_CHAR.length()); } }
		 */
		return _outputBuffer;

	}

	public void processCommandLine(String commandLine) {

		String buffer;
		String template = "";
		ArrayList<String> command = new ArrayList<String>();

		buffer = commandLine.trim();

		if (buffer.indexOf(" ") > 0) {

			command.add(buffer.substring(0, buffer.indexOf(" ")));
			buffer = buffer.substring(buffer.indexOf(" ") + 1);

			if (buffer.indexOf(" ") > 0) {

				command.add(buffer.substring(0, buffer.indexOf(" ")));
				command.add(buffer.substring(buffer.indexOf(" ") + 1));

			} else {

				command.add(buffer);

			}

		} else {

			command.add(commandLine);

		}

		if (command.size() >= 2) {

			String type = "";
			String param = "";

			type = (String) command.get(0);
			param = (String) command.get(1);

			if (type.compareTo("library") == 0) {

				if ((param.compareTo("list") == 0)
						|| (param.compareTo("fetch") == 0)) {

					template = LIBRARY_ID_TAG + "\t" + LIBRARY_PATH_TAG + "\t"
							+ LIBRARY_SCAN_STATUS_TAG + "\r\n";

				} else if (param.compareTo("add") == 0) {

					template = LIBRARY_ID_TAG + "\r\n";

				} else if (param.compareTo("listfailed") == 0) {

					template = LIBRARY_ID_TAG + "\t" + TRACK_FILE_NAME_TAG
							+ "\r\n";

				}

			} else if (type.compareTo("artist") == 0) {

				if ((param.compareTo("list") == 0)
						|| (param.compareTo("fetch") == 0)) {

					template = ARTIST_ID_TAG + "\t" + ARTIST_NAME_TAG + "\t"
							+ ARTIST_ORDER_NAME_TAG + "\r\n";

				} else if (param.compareTo("add") == 0) {

					template = ARTIST_ID_TAG + "\r\n";

				}

			} else if (type.compareTo("album") == 0) {

				if ((param.compareTo("list") == 0)
						|| (param.compareTo("listbyartist") == 0)
						|| (param.compareTo("listbycompartist") == 0)
						|| (param.compareTo("fetch") == 0)) {

					template = ALBUM_ID_TAG + "\t" + ALBUM_NAME_TAG + "\t"
							+ ALBUM_MULTIPLE_ARTIST_ID_TAG + "\r\n";

				}

			} else if (type.compareTo("track") == 0) {

				if ((param.compareTo("list") == 0)
						|| (param.compareTo("listbyartist") == 0)
						|| (param.compareTo("listbyalbum") == 0)
						|| (param.compareTo("listbyartistalbum") == 0)
						|| (param.compareTo("listbylibrary") == 0)
						|| (param.compareTo("listbyplaylist") == 0)
						|| (param.compareTo("listbyqueue") == 0)
						|| (param.compareTo("fetch") == 0)
						|| (param.compareTo("current") == 0)) {

					template = TRACK_ID_TAG + "\t" + LIBRARY_ID_TAG + "\t"
							+ TRACK_FILE_NAME_TAG + "\t" + TRACK_NAME_TAG
							+ "\t" + TRACK_NUMBER_TAG + "\t" + ARTIST_ID_TAG
							+ "\t" + ALBUM_ID_TAG + "\t" + CONTENT_TYPE_ID_TAG
							+ "\r\n";

				}

			} else if (type.compareTo("content_type") == 0) {

				if ((param.compareTo("list") == 0)
						|| (param.compareTo("fetch") == 0)) {

					template = CONTENT_TYPE_ID_TAG + "\t" + CONTENT_TYPE_TAG
							+ "\r\n";

				}

			} else if (type.compareTo("playlist") == 0) {

				if ((param.compareTo("list") == 0)
						|| (param.compareTo("listactive") == 0)
						|| (param.compareTo("listinactive") == 0)
						|| (param.compareTo("fetch") == 0)) {

					template = PLAYLIST_ID_TAG + "\t" + PLAYLIST_NAME_TAG
							+ "\r\n";

				} else if (param.compareTo("add") == 0) {

					template = PLAYLIST_ID_TAG + "\r\n";

				}

			}

		}

		process(template, command, new Properties());

	}

	public void processTemplateFile(String fileName, Properties prop) {

		processTemplateFile(new File(fileName), prop);

	}

	public void processTemplateFile(File file, Properties prop) {

		if (prop.getProperty("library_id", "").length() != 0) {

			Library lib = new Library();

			lib.setLibraryID(Integer.parseInt(prop
					.getProperty("library_id", "")));
			lib.fetch();
			lib.getProperties(prop);

		}

		if (prop.getProperty("artist_id", "").length() != 0) {

			Artist artist = new Artist();

			artist.setArtistID(Integer.parseInt(prop.getProperty("artist_id",
					"")));
			artist.fetch();
			artist.getProperties(prop);

		}

		if (prop.getProperty("album_id", "").length() != 0) {

			Album album = new Album();

			album.setAlbumID(Integer.parseInt(prop.getProperty("album_id",
							"")));
			album.fetch();
			album.getProperties(prop);

		}

		if (prop.getProperty("track_id", "").length() != 0) {

			if (prop.getProperty("track_id", "").indexOf(",") == 0) {

				Track track = new Track();

				track.setTrackID(Integer.parseInt(prop.getProperty("track_id",
						"")));
				track.fetch();
				track.getProperties(prop);

			}

		}

		try {

			FileInputStream fis = new FileInputStream(file);
			processTemplateStream(
					new BufferedReader(new InputStreamReader(fis)), prop);

		} catch (Exception e) {

			System.err.println(e);

		}
		;

	}

	public void processTemplateString(String template, Properties prop) {

		try {

			StringReader sr = new StringReader(template);
			processTemplateStream(new BufferedReader(sr), prop);

		} catch (Exception e) {

			System.err.println(e);

		}
		;
	}

	private void processTemplateStream(BufferedReader br, Properties prop) {

		int loopDepth = 0;
		boolean inElse = false;
		String buffer = "";
		String noResultsBuffer = "";
		String template = "";
		ArrayList<String> command = new ArrayList<String>();

		_outputBuffer = "";

		try {

			String line = br.readLine();

			while (line != null) {

				if (line.indexOf("<loop ") != -1) {
					if (loopDepth == 0) {
						_outputBuffer += processTemplate(prop, buffer);
						buffer = "";
						command = parseLoopString(line, prop);
					} else {
						buffer += line + "\r\n";
					}
					++loopDepth;
				} else if ((line.indexOf("<loopelse>") != -1) && (loopDepth == 1)) {
					inElse = true;
					noResultsBuffer = "";
				} else if (line.indexOf("</loop>") != -1) {
					loopDepth--;
					inElse = false;
					if (loopDepth == 0) {
						template = buffer;
						if (noResultsBuffer.length() != 0) {
							if (command.size() == 3) {
								command.set(2, noResultsBuffer);
							}
							else {
								command.add(noResultsBuffer);
							}
						}
						process(template, command, prop);
						buffer = "";
						noResultsBuffer = "";
					} else {
						buffer += line + "\r\n";
					}

				} else {
					if (!inElse)
						buffer += line + "\r\n";
					else
						noResultsBuffer += line + "\r\n";
				}
				line = br.readLine();

			}

			_outputBuffer += processTemplate(prop, buffer);

		} catch (Exception e) {

			System.err.println(e);

		}
		;

	}

	private void process(String template, ArrayList<String> command, Properties prop) {

		if (command.size() >= 1) {

			String cmd = (String) command.get(0);
			cmd = cmd.replaceAll("/", "").replaceAll(" ", "").toLowerCase().trim();
			if (command.size() > 1) {
				command.set(1, ((String) command.get(1)).replaceAll("/", "").replaceAll(" ", "").toLowerCase().trim());
			}
			
			if (cmd.compareTo("library") == 0) {

				processLibrary(template, command, prop);

			} else if (cmd.compareTo("artist") == 0) {

				processArtist(template, command, prop);

			} else if (cmd.compareTo("track") == 0) {

				processTrack(template, command, prop);

			} else if (cmd.compareTo("album") == 0) {

				processAlbum(template, command, prop);

			} else if (cmd.compareTo("content_type") == 0) {

				processContentType(template, command, prop);

			} else if (cmd.compareTo("playlist") == 0) {

				processPlaylist(template, command, prop);
				
			} else if (cmd.compareTo("playlistrule") == 0) {

				processPlaylistRule(template, command, prop);

			} else if (cmd.compareTo("request") == 0) {

				processRequestQueue(template, command, prop);

			} else if (cmd.compareTo("playpause") == 0) {
                    
                if (!Options.inDemoMode()) 
                    SoundPlayer.getSoundPlayer().playPause();
                else 
                    DemoSoundPlayer.getSoundPlayer().playPause();
				
				
			} else if ((cmd.compareTo("stopsong") == 0) || (cmd.compareTo("stop") == 0) || (cmd.compareTo("skipcurrentsong") == 0)) {

                if (!Options.inDemoMode()) 
                    SoundPlayer.getSoundPlayer().stopSong();
                else 
                    DemoSoundPlayer.getSoundPlayer().stopSong();
			}

		}

	}

	private void processChangePassword(Properties prop) {

		if (Options.verifyAdminPassword(prop.getProperty("oldpassword", ""))) {
			if (prop.getProperty("newpassword", "").equals(
					prop.getProperty("verifypassword", ""))) {
				Options.setAdminPassword(prop.getProperty("newpassword", ""));
				prop.setProperty("message", "Password has been changed");
			} else {
				prop.setProperty("message", "New passwords do not match");
			}
		} else {
			prop.setProperty("message", "Old password is incorrect");
		}

	}

	private void processLibrary(String template, ArrayList command,
			Properties prop) {

		String buffer = "";

		if (command.size() >= 2) {

			String cmd = (String) command.get(1);
			String params;
			if (command.size() >= 3) {
				params = (String) command.get(2);
			} else {
				params = "";
			}

			if (cmd.compareTo("add") == 0) {

				if (params.length() > 0) {

					Library lib = new Library();
					lib.setLibraryPath(params);
					lib.save();
					sendOutput(processTemplate(lib.getProperties(prop),
							template));

				}

			} else if (cmd.compareTo("save") == 0) {

				if (params.length() > 0) {

					Library lib = new Library();
					String[] input = splitString(params, "\t");
					if (input.length >= 2) {

						lib.setLibraryID(Integer.parseInt(input[0]));
						lib.fetch();
						lib.setLibraryPath(input[1]);
						lib.save();

					}

				}

			} else if (cmd.compareTo("scan") == 0) {

				if (params.length() > 0) {

					try {

						Library lib = new Library();
						lib.setLibraryID(Integer.parseInt(params));
						if (lib.fetch()) {

							lib.scan();

						}

					} catch (NumberFormatException e) {
					}

				}

			} else if (cmd.compareTo("scanall") == 0) {

				if (params.length() > 0) {

					try {

						Library lib = new Library();
						lib.setLibraryID(Integer.parseInt(params));
						if (lib.fetch()) {

							lib.scan(true);

						}

					} catch (NumberFormatException e) {
					}

				}

			} else if (cmd.compareTo("clean") == 0) {

				try {

					Library lib = new Library();
					lib.setLibraryID(Integer.parseInt(params));
					if (lib.fetch()) {

						lib.cleanMissingFiles();
						Maintenance maint = new Maintenance();
						maint.cleanDatabase();

					}

				} catch (NumberFormatException e) {
				}

			}

			if (cmd.compareTo("remove") == 0) {

				if (params.length() > 0) {

					try {

						Library lib = new Library();
						lib.setLibraryID(Integer.parseInt(params));
						lib.remove();

					} catch (NumberFormatException e) {
					}

				}

			}

			if (cmd.compareTo("list") == 0) {
				LibraryCollection libcol = new LibraryCollection();
				setRange(libcol, prop);
				libcol.list();

				while (!libcol.isEOF()) {

					buffer = processTemplate(libcol.getFields(prop), template);
					CommandParser cp = new CommandParser();
					cp.processTemplateString(buffer, libcol.getFields(prop));
					sendOutput(cp.getOutput());
					libcol.moveNext();

				}

			} else if (cmd.compareTo("fetch") == 0) {

				if (params.length() > 0) {

					Library lib = new Library();
					lib.setLibraryID(Integer.parseInt(params));
					lib.fetch();
					buffer = processTemplate(lib.getProperties(prop), template);
					CommandParser cp = new CommandParser();
					cp.processTemplateString(buffer, lib.getProperties(prop));
					sendOutput(cp.getOutput());

				}

			} else if (cmd.compareTo("listfailed") == 0) {

				FailedFileCollection ffcol = new FailedFileCollection();
				ffcol.list();

				if (ffcol.isEOF()) {
					sendOutput(params);
				}
				else {
					while (!ffcol.isEOF()) {
	
						buffer = processTemplate(ffcol.getFields(prop), template);
						CommandParser cp = new CommandParser();
						cp.processTemplateString(buffer, ffcol.getFields(prop));
						sendOutput(cp.getOutput());
						ffcol.moveNext();
	
					}
				}
			}

		}

	}

	private void processArtist(String template, ArrayList command,
			Properties prop) {

		String buffer = "";

		if (command.size() >= 2) {

			String cmd = (String) command.get(1);
			String params;
			if (command.size() >= 3) {
				params = (String) command.get(2);
			} else {
				params = "";
			}

			if (cmd.compareTo("add") == 0) {

				if (params.length() > 0) {

					Artist artist = new Artist();
					String input[] = splitString(params, "\t");

					if (input.length >= 1) {

						artist.setArtistName(input[0]);
						if (input.length >= 2) {
							artist.setArtistOrderName(input[1]);
						} else {
							artist.setArtistOrderName("");
						}
						artist.save();

					}
					sendOutput(processTemplate(artist.getProperties(prop),
							template));

				}

			} else if (cmd.compareTo("list") == 0) {

				ArtistCollection artcol = new ArtistCollection();
				setRange(artcol, prop);
				if (params.length() == 0) {

					artcol.list();

				} else {

					artcol.list(params);

				}

				while (!artcol.isEOF()) {

					buffer = processTemplate(artcol.getFields(prop), template);
					CommandParser cp = new CommandParser();
					cp.processTemplateString(buffer, artcol.getFields(prop));
					sendOutput(cp.getOutput());
					artcol.moveNext();

				}

			} else if (cmd.compareTo("remove") == 0) {

				if (params.length() > 0) {

					try {

						Artist artist = new Artist();
						artist.setArtistID(Integer.parseInt(params));
						artist.remove();

					} catch (NumberFormatException e) {
					}

				}

			} else if (cmd.compareTo("save") == 0) {

				if (params.length() > 0) {

					Artist artist = new Artist();
					String input[] = splitString(params, "\t");

					if (input.length >= 3) {

						artist.setArtistID(Integer.parseInt(input[0]));
						artist.setArtistName(input[1]);
						artist.setArtistOrderName(input[2]);
						artist.save();

					}

				}

			} else if (cmd.compareTo("fetch") == 0) {

				if (params.length() > 0) {

					Artist artist = new Artist();
					artist.setArtistID(Integer.parseInt(params));
					artist.fetch();
					buffer = processTemplate(artist.getProperties(prop),
							template);
					CommandParser cp = new CommandParser();
					cp
							.processTemplateString(buffer, artist
									.getProperties(prop));
					sendOutput(cp.getOutput());

				}

			}

		}

	}

	private void processAlbum(String template, ArrayList command,
			Properties prop) {

		String buffer = "";

		if (command.size() >= 2) {

			String cmd = (String) command.get(1);
			String params;
			if (command.size() >= 3) {
				params = (String) command.get(2);
			} else {
				params = "";
			}

			if (cmd.substring(0, 4).compareTo("list") == 0) {

				AlbumCollection albumcol = new AlbumCollection();
				setRange(albumcol, prop);
				if (params.length() == 0) {

					if (cmd.compareTo("listcomps") == 0) {

						albumcol.listCompilations();

					} else if (cmd.compareTo("listpossiblecomps") == 0) {

						albumcol.listPossibleCompilations();

					} else {

						albumcol.list(0, 0);

					}

				} else {

					if (cmd.compareTo("list") == 0) {

						albumcol.list(params, 0, 0);

					} else if (cmd.compareTo("listbyartist") == 0) {

						albumcol.listByArtist(Integer.parseInt(params), 0, 0);

					} else if (cmd.compareTo("listbycompartist") == 0) {

						albumcol.listByCompArtist(Integer.parseInt(params), 0, 0);

					}

				}

				while (!albumcol.isEOF()) {

					buffer = processTemplate(albumcol.getFields(prop), template);
					CommandParser cp = new CommandParser();
					cp.processTemplateString(buffer, albumcol.getFields(prop));
					sendOutput(cp.getOutput());
					albumcol.moveNext();

				}

			} else if (cmd.compareTo("remove") == 0) {

				if (params.length() > 0) {

					try {

						Album album = new Album();
						album.setAlbumID(Integer.parseInt(params));
						album.remove();

					} catch (NumberFormatException e) {
					}

				}

			} else if (cmd.compareTo("save") == 0) {

				if (params.length() > 0) {

					Album album = new Album();
					String input[] = splitString(params, "\t");

					if (input.length >= 3) {

						album.setAlbumID(Integer.parseInt(input[0]));
						album.setAlbumName(input[1]);
						album.setMultipleArtistID(Integer.parseInt(input[2]));
						album.save();

					}

				}

			} else if (cmd.compareTo("splitalbum") == 0) {

				if (params.length() > 0) {

					Album album = new Album();

					album.setAlbumID(Integer.parseInt(params));
					album.split();

				}

			} else if (cmd.compareTo("fetch") == 0) {

				if (params.length() > 0) {

					Album album = new Album();
					album.setAlbumID(Integer.parseInt(params));
					album.fetch();
					buffer = processTemplate(album.getProperties(prop),
							template);
					CommandParser cp = new CommandParser();
					cp.processTemplateString(buffer, album.getProperties(prop));
					sendOutput(cp.getOutput());

				}

			}

		}

	}

	private void processTrack(String template, ArrayList command,
			Properties prop) {

		String buffer = "";

		if (command.size() >= 2) {

			String cmd = (String) command.get(1);
			String params;
			if (command.size() >= 3) {
				params = (String) command.get(2);
			} else {
				params = "";
			}

			if (cmd.substring(0, 4).compareTo("list") == 0) {

				TrackCollection trackcol = new TrackCollection();
				setRange(trackcol, prop);
				if (params.length() == 0) {

					if (cmd.compareTo("listbyqueue") == 0) {

						trackcol.listByQueue();

					} else {

						trackcol.list(0, 0);

					}

				} else {

					if (cmd.compareTo("list") == 0) {

						trackcol.list(params, 0, 0);

					} else if (cmd.compareTo("listbylibrary") == 0) {

						trackcol.listByLibrary(Integer.parseInt(params));

					} else if (cmd.compareTo("listbyartist") == 0) {

						trackcol.listByArtist(Integer.parseInt(params), 0, 0);

					} else if (cmd.compareTo("listbyalbum") == 0) {

						trackcol.listByAlbum(Integer.parseInt(params), 0, 0);

					} else if (cmd.compareTo("listbyartistalbum") == 0) {

						trackcol.listByArtistAlbum(params, 0, 0);

					} else if (cmd.compareTo("listbyplaylist") == 0) {

						trackcol.listByPlaylist(Integer.parseInt(params), 0, 0);

					}

				}

				while (!trackcol.isEOF()) {

					buffer = processTemplate(trackcol.getFields(prop), template);
					CommandParser cp = new CommandParser();
					cp.processTemplateString(buffer, trackcol.getFields(prop));
					sendOutput(cp.getOutput());
					trackcol.moveNext();

				}

			} else if (cmd.compareTo("remove") == 0) {

				if (params.length() > 0) {

					try {

						Track track = new Track();
						track.setTrackID(Integer.parseInt(params));
						track.remove();

					} catch (NumberFormatException e) {
					}

				}

			} else if (cmd.compareTo("save") == 0) {

				if (params.length() > 0) {

					Track track = new Track();
					String input[] = splitString(params, "\t");

					if (input.length >= 3) {

						track.setTrackID(Integer.parseInt(input[0]));
						track.setLibraryID(Integer.parseInt(input[1]));
						track.setFileName(input[2]);
						track.setTrackName(input[3]);
						track.setTrackNumber(Integer.parseInt(input[4]));
						track.setArtistID(Integer.parseInt(input[5]));
						track.setAlbumID(Integer.parseInt(input[6]));
						track.setContentTypeID(Integer.parseInt(input[7]));
						track.save();

					}

				}

			} else if (cmd.compareTo("fetch") == 0) {

				if (params.length() > 0) {

					Track track = new Track();
					track.setTrackID(Integer.parseInt(params));
					track.fetch();
					sendOutput(processTemplate(track.getProperties(prop),
							template));

				}

			} else if (cmd.compareTo("current") == 0) {

                if ((!Options.inDemoMode() && (SoundPlayer.getSoundPlayer().currentTrackID != -1)) ||
                    (Options.inDemoMode() && (DemoSoundPlayer.getSoundPlayer().currentTrackID != -1))) {

					Track track = new Track();
                    if (!Options.inDemoMode()) 
                        track.setTrackID(SoundPlayer.getSoundPlayer().currentTrackID);
                    else
                        track.setTrackID(DemoSoundPlayer.getSoundPlayer().currentTrackID);
					track.fetch();
					sendOutput(processTemplate(track.getProperties(prop),
							template));

				} else if (params.length() > 0) {

					sendOutput(params);

				}

			}

		}

	}

	private void processContentType(String template, ArrayList command,
			Properties prop) {

		String buffer = "";

		if (command.size() >= 2) {

			String cmd = (String) command.get(1);
			String params;
			if (command.size() >= 3) {
				params = (String) command.get(2);
			} else {
				params = "";
			}

			if (cmd.compareTo("save") == 0) {

				if (params.length() > 0) {

					ContentType content = new ContentType();
					String[] input = splitString(params, "\t");
					if (input.length >= 2) {

						content.setContentTypeID(Integer.parseInt(input[0]));
						content.fetch();
						content.setContentType(input[1]);
						content.save();

					}

				}

			} else if (cmd.compareTo("list") == 0) {
				ContentTypeCollection contentcol = new ContentTypeCollection();
				setRange(contentcol, prop);
				contentcol.list();

				while (!contentcol.isEOF()) {

					buffer = processTemplate(contentcol.getFields(prop),
							template);
					CommandParser cp = new CommandParser();
					cp
							.processTemplateString(buffer, contentcol
									.getFields(prop));
					sendOutput(cp.getOutput());
					contentcol.moveNext();

				}

			} else if (cmd.compareTo("fetch") == 0) {

				if (params.length() > 0) {

					ContentType content = new ContentType();
					content.setContentTypeID(Integer.parseInt(params));
					content.fetch();
					sendOutput(processTemplate(content.getProperties(prop),
							template));

				}

			}

		}

	}

	private void processPlaylist(String template, ArrayList command,
			Properties prop) {

		String buffer = "";

		if (command.size() >= 2) {

			String cmd = (String) command.get(1);
			String params;
			if (command.size() >= 3) {
				params = (String) command.get(2);
			} else {
				params = "";
			}

			if (cmd.compareTo("add") == 0) {

				if (params.length() > 0) {

					Playlist list = new Playlist();
					list.setPlaylistName(params);
					list.save();
					sendOutput(processTemplate(list.getProperties(prop),
							template));

				}

			} else if (cmd.compareTo("save") == 0) {

				if (params.length() > 0) {

					Playlist list = new Playlist();
					String[] input = splitString(params, "\t");
					if (input.length >= 2) {

						list.setPlaylistID(Integer.parseInt(input[0]));
						list.setPlaylistName(input[1]);
						list.save();

					}

				}

			} else if (cmd.compareTo("list") == 0) {
				PlaylistCollection listcol = new PlaylistCollection();
				setRange(listcol, prop);
				listcol.list();

				while (!listcol.isEOF()) {

					buffer = processTemplate(listcol.getFields(prop), template);
					CommandParser cp = new CommandParser();
					cp.processTemplateString(buffer, listcol.getFields(prop));
					sendOutput(cp.getOutput());
					listcol.moveNext();

				}

			} else if (cmd.compareTo("listactive") == 0) {
				PlaylistCollection listcol = new PlaylistCollection();
				setRange(listcol, prop);
				listcol.listActive();

				if (listcol.isEOF()) {
					sendOutput(params);
				}
				else {
					while (!listcol.isEOF()) {
	
						buffer = processTemplate(listcol.getFields(prop), template);
						CommandParser cp = new CommandParser();
						cp.processTemplateString(buffer, listcol.getFields(prop));
						sendOutput(cp.getOutput());
						listcol.moveNext();
	
					}
				}
			} else if (cmd.compareTo("listinactive") == 0) {
				PlaylistCollection listcol = new PlaylistCollection();
				setRange(listcol, prop);
				listcol.listInactive();

				if (listcol.isEOF()) {
					sendOutput(params);
				}
				else {
					while (!listcol.isEOF()) {
	
						buffer = processTemplate(listcol.getFields(prop), template);
						CommandParser cp = new CommandParser();
						cp.processTemplateString(buffer, listcol.getFields(prop));
						sendOutput(cp.getOutput());
						listcol.moveNext();
	
					}
				}
				
			} else if (cmd.compareTo("remove") == 0) {

				if (params.length() > 0) {

					try {

						Playlist list = new Playlist();
						list.setPlaylistID(Integer.parseInt(params));
						list.remove();

					} catch (NumberFormatException e) {
					}

				}

			} else if (cmd.compareTo("fetch") == 0) {

				if (params.length() > 0) {

					Playlist list = new Playlist();
					list.setPlaylistID(Integer.parseInt(params));
					list.fetch();
					buffer = processTemplate(list.getProperties(prop), template);
					CommandParser cp = new CommandParser();
					cp.processTemplateString(buffer, list.getProperties(prop));
					sendOutput(cp.getOutput());

				}

			} else if (cmd.compareTo("addtrack") == 0) {

				if (params.length() > 0) {

					String input[] = splitString(params, ",");

					if (input.length >= 2) {

						Playlist list = new Playlist();
						list.setPlaylistID(Integer.parseInt(input[0]));
						list.addTrack(Integer.parseInt(input[1]));

					}

				}

			} else if (cmd.compareTo("addalbum") == 0) {

				if (params.length() > 0) {

					String input[] = splitString(params, ",");

					if (input.length >= 2) {

						Playlist list = new Playlist();
						list.setPlaylistID(Integer.parseInt(input[0]));
						list.addAlbum(Integer.parseInt(input[1]));

					}

				}

			} else if (cmd.compareTo("addartist") == 0) {

				if (params.length() > 0) {

					String input[] = splitString(params, ",");

					if (input.length >= 2) {

						Playlist list = new Playlist();
						list.setPlaylistID(Integer.parseInt(input[0]));
						list.addArtist(Integer.parseInt(input[1]));

					}

				}

			} else if (cmd.compareTo("removetrack") == 0) {

				if (params.length() > 0) {

					String input[] = splitString(params, ",");

					if (input.length >= 2) {

						Playlist list = new Playlist();
						list.setPlaylistID(Integer.parseInt(input[0]));
						input[0] = "";
						list.removeTracks(input);

					}

				}

			} else if (cmd.compareTo("empty") == 0) {

				if (params.length() > 0) {

					try {

						Playlist list = new Playlist();
						list.setPlaylistID(Integer.parseInt(params));
						list.empty();

					} catch (NumberFormatException e) {
					}

				}

			} else if (cmd.compareTo("activate") == 0) {

				if (params.length() > 0) {

					try {

						Playlist list = new Playlist();
						list.setPlaylistID(Integer.parseInt(params));
						list.activate(true);

					} catch (NumberFormatException e) {
					}

				}

			} else if (cmd.compareTo("deactivate") == 0) {

				try {

					Playlist list = new Playlist();
					if (params.length() > 0) {
						list.setPlaylistID(Integer.parseInt(params));
						list.activate(false);
					} else {
						list.deactivateall();
					}

				} catch (NumberFormatException e) {
				}

			} 

		}

	}

	private void processPlaylistRule(String template, ArrayList command,
			Properties prop) {

		String buffer = "";

		if (command.size() >= 2) {

			String cmd = (String) command.get(1);
			String params;
			if (command.size() >= 3) {
				params = (String) command.get(2);
			} else {
				params = "";
			}

			if (cmd.length() >= 4 && cmd.substring(0, 4).compareTo("list") == 0) {

				PlaylistRuleCollection rulecol = new PlaylistRuleCollection();

				if (cmd.compareTo("listbyplaylist") == 0) {

					rulecol.list(Integer.parseInt(params));

				}

				while (!rulecol.isEOF()) {

					buffer = processTemplate(rulecol.getFields(prop), template);
					CommandParser cp = new CommandParser();
					cp.processTemplateString(buffer, rulecol.getFields(prop));
					sendOutput(cp.getOutput());
					rulecol.moveNext();

				}
			}
			else if (cmd.compareTo("add") == 0) {
				
				if (prop.getProperty("param").equals("Add Library Rule")) {

					if (prop.containsKey("playlist_id") && prop.containsKey("lib_rule_value")) {
	
						Playlist list = new Playlist();
						list.setPlaylistID(Integer.parseInt(prop.getProperty("playlist_id")));
						list.addRule("library", prop.getProperty("lib_rule_value"));
	
					}
				}
				else if (prop.getProperty("param").equals("Add Artist Rule")) {

					if (prop.containsKey("playlist_id") && prop.containsKey("artist_rule_value")) {
	
						Artist artist = new Artist();
						artist.setArtistName(prop.getProperty("artist_rule_value"));
						if (artist.find()) {
							Playlist list = new Playlist();
							list.setPlaylistID(Integer.parseInt(prop.getProperty("playlist_id")));
							list.addRule("artist", artist.getArtistID());
						}
	
					}
				}
				else if (prop.getProperty("param").equals("Add Genre Rule")) {

					if (prop.containsKey("playlist_id") && prop.containsKey("genre_rule_value")) {
	
						Playlist list = new Playlist();
						list.setPlaylistID(Integer.parseInt(prop.getProperty("playlist_id")));
						list.addRule("genre", prop.getProperty("genre_rule_value"));
	
					}
				}
				
			} else if (cmd.compareTo("remove") == 0) {

				if (prop.containsKey("playlist_rule_id")) {

					String input[] = splitString(prop.getProperty("playlist_rule_id"), ",");

					if (input.length >= 1) {

						Playlist list = new Playlist();
						list.setPlaylistID(Integer.parseInt(prop.getProperty("playlist_id")));
						list.removeRules(input);

					}

				}
			}

		}

	}

	private void processRequestQueue(String template, ArrayList command,
			Properties prop) {

		if (command.size() >= 2) {

			String cmd = (String) command.get(1);
			String params;
			if (command.size() >= 3) {
				params = (String) command.get(2);
			} else {
				params = "";
			}

			if (cmd.compareTo("addtrack") == 0) {

				if (params.length() > 0) {

					RequestQueue queue = new RequestQueue();
					queue.requestTrack(Integer.parseInt(params));

				}

			} else if (cmd.compareTo("addalbum") == 0) {

				if (params.length() > 0) {

					RequestQueue queue = new RequestQueue();
					queue.requestAlbum(Integer.parseInt(params));

				}

			} else if (cmd.compareTo("addartist") == 0) {

				if (params.length() > 0) {

					RequestQueue queue = new RequestQueue();
					queue.requestArtist(Integer.parseInt(params));

				}

			} else if (cmd.compareTo("empty") == 0) {

				RequestQueue queue = new RequestQueue();
				queue.empty();

			} else if (cmd.compareTo("remove") == 0) {

				if (params.length() > 0) {

					String input[] = splitString(params, ",");

					if (input.length >= 1) {

						RequestQueue queue = new RequestQueue();
						queue.removeRequests(input);

					}

				}

			}

		}

	}

	private String processTemplate(Properties prop, String template) {

		String output = "";
		String buffer = "";

		try {

			StringReader sr = new StringReader(template);
			BufferedReader br = new BufferedReader(sr);
			int loopDepth = 0;

			String line = br.readLine();

			while (line != null) {

				if (line.indexOf("<loop ") != -1) {
					if (loopDepth == 0) {
						output += replaceFields(prop, buffer);
						buffer = "";
					}
					buffer += line + "\r\n";
					++loopDepth;
				} else if (line.indexOf("</loop>") != -1) {
					loopDepth--;
					buffer += line + "\r\n";
					if (loopDepth == 0) {
						output += buffer;
						buffer = "";
					}
				} else {

					buffer += line + "\r\n";

				}
				line = br.readLine();

			}

			output += replaceFields(prop, buffer);

		} catch (Exception e) {

			System.err.println(e);

		}
		;

		return output;

	}

	private String replaceFields(Properties prop, String template) {

		String buffer;
		String temp;

		buffer = template;

		while (buffer.indexOf(VARIABLE_BEGIN_TAG) != -1) {

			int beg = buffer.indexOf(VARIABLE_BEGIN_TAG);
			int end = buffer.indexOf(VARIABLE_END_TAG, beg);

			String varName = buffer.substring(
					beg + VARIABLE_BEGIN_TAG.length(), end);

			buffer = replaceString(buffer, buffer.substring(beg, end
					+ VARIABLE_END_TAG.length()), prop.getProperty(varName, ""));

		}

		buffer = replaceString(buffer, MESSAGE_TAG, prop.getProperty("message",
				""));

		buffer = replaceString(buffer, LIBRARY_ID_TAG, prop.getProperty(
				"library_id", "-1"));
		if (buffer.indexOf(LIBRARY_PATH_TAG) != -1) {
			temp = prop.getProperty("library_path", "");
			if (temp.compareTo("") == 0) {
				Library library = new Library();
				library.setLibraryID(Integer.parseInt(prop.getProperty(
						"library_id", "-1")));
				library.fetch();
				temp = library.getLibraryPath();
			}
			buffer = replaceString(buffer, LIBRARY_PATH_TAG, temp);
		}
		if (buffer.indexOf(LIBRARY_SCAN_STATUS_TAG) != -1) {
			temp = prop.getProperty("scan_status", "");
			if (temp.compareTo("") == 0) {
				Library library = new Library();
				library.setLibraryID(Integer.parseInt(prop.getProperty(
						"library_id", "-1")));
				library.fetch();
				temp = String.valueOf(library.getScanStatus());
			}
			buffer = replaceString(buffer, LIBRARY_SCAN_STATUS_TAG, temp);
		}
		if (buffer.indexOf(LIBRARY_SCAN_STATUS_STRING_TAG) != -1) {
			temp = prop.getProperty("scan_status_string", "");
			if (temp.compareTo("") == 0) {
				Library library = new Library();
				library.setLibraryID(Integer.parseInt(prop.getProperty(
						"library_id", "-1")));
				library.fetch();
				temp = library.getScanStatusString();
			}
			buffer = replaceString(buffer, LIBRARY_SCAN_STATUS_STRING_TAG, temp);
		}
		if (buffer.indexOf(LIBRARY_TRACK_COUNT_TAG) != -1) {
			Library library = new Library();
			library.setLibraryID(Integer.parseInt(prop.getProperty(
					"library_id", "-1")));
			library.fetch();
			buffer = replaceString(buffer, LIBRARY_TRACK_COUNT_TAG, String
					.valueOf(library.getTrackCount()));
		}

		buffer = replaceString(buffer, ARTIST_ID_TAG, prop.getProperty(
				"artist_id", "-1"));
		if (buffer.indexOf(ARTIST_NAME_TAG) != -1) {
			temp = prop.getProperty("artist_name", "");
			if (temp.compareTo("") == 0) {
				Artist artist = new Artist();
				artist.setArtistID(Integer.parseInt(prop.getProperty(
						"artist_id", "-1")));
				artist.fetch();
				temp = artist.getArtistName();
			}
			buffer = replaceString(buffer, ARTIST_NAME_TAG, temp);
		}
		if (buffer.indexOf(ARTIST_ORDER_NAME_TAG) != -1) {
			temp = prop.getProperty("artist_order_name", "");
			if (temp.compareTo("") == 0) {
				Artist artist = new Artist();
				artist.setArtistID(Integer.parseInt(prop.getProperty(
						"artist_id", "-1")));
				artist.fetch();
				temp = artist.getArtistOrderName();
			}
			buffer = replaceString(buffer, ARTIST_ORDER_NAME_TAG, temp);
		}

		buffer = replaceString(buffer, ALBUM_ID_TAG, prop.getProperty(
				"album_id", "-1"));
		if (buffer.indexOf(ALBUM_NAME_TAG) != -1) {
			temp = prop.getProperty("album_name", "");
			if (temp.compareTo("") == 0) {
				Album album = new Album();
				album.setAlbumID(Integer.parseInt(prop.getProperty("album_id",
						"-1")));
				album.fetch();
				temp = album.getAlbumName();
			}
			buffer = replaceString(buffer, ALBUM_NAME_TAG, temp);
		}
		if (buffer.indexOf(ALBUM_MULTIPLE_ARTIST_ID_TAG) != -1) {
			temp = prop.getProperty("multiple_artist_id", "-1");
			if (temp.compareTo("-1") == 0) {
				Album album = new Album();
				album.setAlbumID(Integer.parseInt(prop.getProperty("album_id",
						"-1")));
				album.fetch();
				temp = String.valueOf(album.getMultipleArtistID());
			}
			buffer = replaceString(buffer, ALBUM_MULTIPLE_ARTIST_ID_TAG, temp);
		}
		if ((buffer.indexOf(ALBUM_ARTIST_ID_TAG) != -1)
				|| (buffer.indexOf(ALBUM_ARTIST_NAME_TAG) != -1)) {

			Album album = new Album();
			album.setAlbumID(Integer.parseInt(prop
					.getProperty("album_id", "-1")));
			album.fetch();
			int artistID = album.findArtistID();
			if (artistID != -1) {

				Artist artist = new Artist();
				artist.setArtistID(artistID);
				artist.fetch();
				buffer = replaceString(buffer, ALBUM_ARTIST_ID_TAG, String
						.valueOf(artist.getArtistID()));
				buffer = replaceString(buffer, ALBUM_ARTIST_NAME_TAG, artist
						.getArtistName());

			} else {

				buffer = replaceString(buffer, ALBUM_ARTIST_ID_TAG, "-1");
				buffer = replaceString(buffer, ALBUM_ARTIST_NAME_TAG, Options
						.getVariousArtist());

			}

		}

		buffer = replaceString(buffer, TRACK_ID_TAG, prop.getProperty(
				"track_id", "-1"));
		if (buffer.indexOf(TRACK_NAME_TAG) != -1) {
			temp = prop.getProperty("track_name", "");
			if (temp.compareTo("") == 0) {
				Track track = new Track();
				track.setTrackID(Integer.parseInt(prop.getProperty("track_id",
						"-1")));
				track.fetch();
				temp = track.getTrackName();
			}
			buffer = replaceString(buffer, TRACK_NAME_TAG, temp);
		}
		if (buffer.indexOf(TRACK_FILE_NAME_TAG) != -1) {
			temp = prop.getProperty("file_name", "");
			if (temp.compareTo("") == 0) {
				Track track = new Track();
				track.setTrackID(Integer.parseInt(prop.getProperty("track_id",
						"-1")));
				track.fetch();
				temp = track.getFileName();
			}
			buffer = replaceString(buffer, TRACK_FILE_NAME_TAG, temp);
		}
		if (buffer.indexOf(TRACK_NUMBER_TAG) != -1) {
			temp = prop.getProperty("track_number", "");
			if (temp.compareTo("") == 0) {
				Track track = new Track();
				track.setTrackID(Integer.parseInt(prop.getProperty("track_id",
						"-1")));
				track.fetch();
				temp = String.valueOf(track.getTrackNumber());
			}
			buffer = replaceString(buffer, TRACK_NUMBER_TAG, temp);
		}
		if (buffer.indexOf(TRACK_ARTIST_NAME_TAG) != -1) {
			Artist artist = new Artist();
			artist.setArtistID(Integer.parseInt(prop.getProperty("artist_id",
					"-1")));
			artist.fetch();
			temp = String.valueOf(artist.getArtistName());
			buffer = replaceString(buffer, TRACK_ARTIST_NAME_TAG, temp);
		}

		buffer = replaceString(buffer, CONTENT_TYPE_ID_TAG, prop.getProperty(
				"content_type_id", "-1"));
		if (buffer.indexOf(CONTENT_TYPE_TAG) != -1) {
			temp = prop.getProperty("content_type", "");
			if (temp.compareTo("") == 0) {
				ContentType content = new ContentType();
				content.setContentTypeID(Integer.parseInt(prop.getProperty(
						"content_type_id", "-1")));
				content.fetch();
				temp = String.valueOf(content.getContentType());
			}
			buffer = replaceString(buffer, CONTENT_TYPE_TAG, temp);
		}

		buffer = replaceString(buffer, PLAYLIST_ID_TAG, prop.getProperty(
				"playlist_id", "-1"));
		if (buffer.indexOf(PLAYLIST_NAME_TAG) != -1) {
			temp = prop.getProperty("playlist_name", "");
			if (temp.compareTo("") == 0) {
				Playlist playlist = new Playlist();
				playlist.setPlaylistID(Integer.parseInt(prop.getProperty(
						"playlist_id", "-1")));
				playlist.fetch();
				temp = String.valueOf(playlist.getPlaylistName());
			}
			buffer = replaceString(buffer, PLAYLIST_NAME_TAG, temp);
		}
		
		buffer = replaceString(buffer, PLAYLIST_RULE_ID_TAG, prop.getProperty(
				"playlist_rule_id", "-1"));
		buffer = replaceString(buffer, PLAYLIST_RULE_TYPE_TAG, prop.getProperty("rule_type", ""));
		buffer = replaceString(buffer, PLAYLIST_RULE_VALUE_TAG, prop.getProperty("rule_value", ""));
		buffer = replaceString(buffer, PLAYLIST_RULE_DISPLAY_VALUE_TAG, prop.getProperty("rule_display_value", ""));
			
		return buffer;

	}

	// we need this because replaceAll is only in 1.4
	private String replaceString(String oldString, String find, String replace) {

		int nIndex = oldString.indexOf(find);

		if (nIndex != -1) {

			return replaceString(oldString.substring(0, nIndex) + replace
					+ oldString.substring(nIndex + find.length()), find,
					replace);

		} else {

			return oldString;

		}

	}

	private void sendOutput(String output) {

		_outputBuffer += output;

	}

	private String[] splitString(String input, String sep) {

		StringTokenizer st = new StringTokenizer(input, sep);
		String[] t = new String[st.countTokens()];
		while (st.hasMoreTokens())
			t[t.length - st.countTokens()] = st.nextToken();

		return t;

	}

	private ArrayList<String> parseLoopString(String input, Properties prop) {

		String buffer = "";
		ArrayList<String> command = new ArrayList<String>();

		if (input.indexOf("type=\"") != -1) {

			buffer = input.substring(input.indexOf("type=\"") + 6);
			buffer = buffer.substring(0, buffer.indexOf("\""));

			command.add(buffer);

			if (input.indexOf("param=\"") != -1) {

				buffer = input.substring(input.indexOf("param=\"") + 7);
				buffer = buffer.substring(0, buffer.indexOf("\""));

				command.add(buffer);

				if (input.indexOf("value=\"") != -1) {

					buffer = input.substring(input.indexOf("value=\"") + 7);
					buffer = buffer.substring(0, buffer.indexOf("\""));
					command.add(replaceVars(buffer, prop));

				}

			}

		}

		return command;

	}

	private String replaceVars(String buffer, Properties prop) {

		int beg = 0;
		int loc = buffer.indexOf(",");
		String cmd = "";
		String output = "";

		while (loc != -1) {

			cmd = buffer.substring(beg, loc);

			beg = loc + 1;
			loc = buffer.indexOf(",", beg);

			if (cmd.charAt(0) == '@') {

				output += prop.getProperty(cmd.substring(1), "") + ",";

			} else {

				output += cmd + ",";

			}

		}

		if (buffer.substring(beg).charAt(0) == '@') {

			output += prop.getProperty(buffer.substring(beg + 1), "");

		} else {

			output += buffer.substring(beg);

		}

		return output;
	}

	public void processForm(Properties prop) {

		String command = prop.getProperty("command", "");
		String param = prop.getProperty("param", "").replaceAll("/", " ").toLowerCase();
		String value = prop.getProperty("value", "");
		
		if (param.indexOf(' ') > 0) {
			param = param.substring(0, param.indexOf(' '));
		}

		if (value.length() == 0) {

			if (command.compareTo("library") == 0) {
				if (param.compareTo("add") == 0) {
					value = prop.getProperty("library_path", "");
				} else if (param.compareTo("save") == 0) {
					value = prop.getProperty("library_id", "-1").concat("\t")
							.concat(prop.getProperty("library_path", ""));
				} else if (param.compareTo("scan") == 0) {
					value = prop.getProperty("library_id", "-1");
				} else if (param.compareTo("remove") == 0) {
					value = prop.getProperty("library_id", "-1");
				} else if (param.compareTo("scanall") == 0) {
					value = prop.getProperty("library_id", "-1");
				} else if (param.compareTo("clean") == 0) {
					value = prop.getProperty("library_id", "-1");
				}
			} else if (command.compareTo("artist") == 0) {
				if (param.compareTo("save") == 0) {
					value = prop.getProperty("artist_id", "-1").concat("\t")
							.concat(prop.getProperty("artist_name", ""))
							.concat("\t").concat(
									prop.getProperty("artist_order_name", ""));
				} else if (param.compareTo("remove") == 0) {
					value = prop.getProperty("artist_id", "-1");
				} else if (param.compareTo("add") == 0) {
					value = prop.getProperty("artist_name", "").concat("\t")
							.concat(prop.getProperty("artist_order_name", ""));
				}
			} else if (command.compareTo("album") == 0) {
				if (param.compareTo("save") == 0) {
					value = prop.getProperty("album_id", "-1").concat("\t")
							.concat(prop.getProperty("album_name", "")).concat(
									"\t").concat(
									prop
											.getProperty("multiple_artist_id",
													"-1"));
				} else if (param.compareTo("remove") == 0) {
					value = prop.getProperty("album_id", "-1");
				} else if (param.compareTo("split") == 0) {
					value = prop.getProperty("album_id", "-1");
				}
			} else if (command.compareTo("track") == 0) {
				if (param.compareTo("save") == 0) {
					value = prop.getProperty("track_id", "-1").concat("\t")
							.concat(prop.getProperty("library_id", "-1"))
							.concat("\t").concat(
									prop.getProperty("file_name", "")).concat(
									"\t").concat(
									prop.getProperty("track_name", "")).concat(
									"\t").concat(
									prop.getProperty("track_number", ""))
							.concat("\t").concat(
									prop.getProperty("artist_id", "-1"))
							.concat("\t").concat(
									prop.getProperty("album_id", "-1")).concat(
									"\t").concat(
									prop.getProperty("content_type_id", "-1"));
				} else if (param.compareTo("remove") == 0) {
					value = prop.getProperty("track_id");
				}
			} else if (command.compareTo("content_type") == 0) {
				if (param.compareTo("save") == 0) {
					value = prop.getProperty("content_type_id", "-1").concat(
							"\t").concat(prop.getProperty("content_type", ""));
				}
			} else if (command.compareTo("playlist") == 0) {
				if (param.compareTo("add") == 0) {
					value = prop.getProperty("playlist_name", "");
				} else if (param.compareTo("save") == 0) {
					value = prop.getProperty("playlist_id", "-1").concat("\t")
							.concat(prop.getProperty("playlist_name", ""));
				} else if (param.compareTo("addtrack") == 0) {
					value = prop.getProperty("playlist_id", "-1").concat(",")
							.concat(prop.getProperty("track_id", "-1"));
				} else if (param.compareTo("addalbum") == 0) {
					value = prop.getProperty("playlist_id", "-1").concat(",")
							.concat(prop.getProperty("album_id", "-1"));
				} else if (param.compareTo("addartist") == 0) {
					value = prop.getProperty("playlist_id", "-1").concat(",")
							.concat(prop.getProperty("artist_id", "-1"));
				} else if (param.compareTo("empty") == 0) {
					value = prop.getProperty("playlist_id", "-1");
				} else if (param.compareTo("remove") == 0) {
					value = prop.getProperty("playlist_id", "-1");
				} else if (param.compareTo("activate") == 0) {
					value = prop.getProperty("playlist_id", "-1");
				} else if (param.compareTo("deactivate") == 0) {
					value = prop.getProperty("playlist_id", "");
				} else if (param.compareTo("removetrack") == 0) {
					value = prop.getProperty("playlist_id", "-1").concat(",")
							.concat(prop.getProperty("track_id", "-1"));
				}
			} else if (command.compareTo("request") == 0) {
				if (param.compareTo("addtrack") == 0) {
					value = prop.getProperty("track_id", "-1");
				} else if (param.compareTo("addalbum") == 0) {
					value = prop.getProperty("album_id", "-1");
				} else if (param.compareTo("addartist") == 0) {
					value = prop.getProperty("artist_id", "-1");
				}

			} else if (command.compareTo("changepassword") == 0) {
				processChangePassword(prop);
				return;
			}

		}

		ArrayList<String> cmd = new ArrayList<String>();
		if (command.length() != 0)
			cmd.add(command);
		if (param.length() != 0)
			cmd.add(param);
		if (value.length() != 0)
			cmd.add(value);
		process("", cmd, prop);
		//(command + " " + param + " " + value);

	}

	private void setRange(BVDatabaseCollection listObject, Properties prop) {

		listObject.setLimit(Integer.parseInt(prop.getProperty("start", "0")),
				Integer.parseInt(prop.getProperty("offset", "0")));

	}
}
